package com.example.springcloudconfig.web;


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AmqpAction {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("sendWork")
    String sendWork(String data){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work",data+i);
        }

        return "工作模式消息发送完成";
    }

    @RequestMapping("sendFanout")
    String sendFanout(String data){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("fanout","",data+i);
        }

        return "广播模式消息发送完成";
    }


    ///////////////////////////////////////////////////////////////
    @RequestMapping("sendDirect/{key}")
    String sendDirect(String data,
                      @PathVariable String key){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("direct", key,
                    key + "=>" + data + i);
        }
        return "定向模式消息发送完成";
    }

    ///////////////////////////////////////////////////////////////
    @RequestMapping("sendTopic/{key}")
    String sendTopic(String data,
                     @PathVariable String key){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("topic", key,
                    key + "=>" + data + i);
        }
        return "主题模式消息发送完成";
    }






    //    @RequestMapping("recvWork2")
    @RabbitListener(queuesToDeclare = @Queue("work"))
    void recvWork2(String data){
        System.out.println("处理工作模式消息2:" + data);
//        return "处理工作模式消息:" + data;
    }

    ///////////////////////////////////////////////////////////////
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value="fanout2"),
            exchange = @Exchange(value = "fanout",
                    type = ExchangeTypes.FANOUT)
    ))
    void recvFanout2(String data){
        System.out.println("处理广播模式消息2:" + data);

    }



    ///////////////////////////////////////////////////////////////
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value="direct2"),
            exchange = @Exchange(value = "direct",
                    type = ExchangeTypes.DIRECT),
            key = "user"
    ))
    void recvDierct2(String data){
        System.out.println("处理定向模式消息2:" + data);
//        return "处理工作模式消息:" + data;
    }


    ///////////////////////////////////////////////////////////////
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value="topic2"),
            exchange = @Exchange(value = "topic",
                    type = ExchangeTypes.TOPIC),
            key = "user.save"
    ))
    void recvTopic2(String data){
        System.out.println("处理主题模式消息2:" + data);
//        return "处理工作模式消息:" + data;
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value="topic1"),
            exchange = @Exchange(value = "topic",
                    type = ExchangeTypes.TOPIC),
            key = "user.#"
    ))
    void recvTopic1(String data){
        System.out.println("处理主题模式消息1:" + data);
//        return "处理工作模式消息:" + data;
    }





}

